package net.bolue.console.data.model;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import net.bolue.console.data.RepInfo;
import net.bolue.console.model.BusiBase;
import net.bolue.sysmng.exception.BolueException;
import net.bolue.sysmng.model.ReportResModel;
import net.bolue.sysmng.tblbean.RepCnd;
import net.bolue.sysmng.tblbean.RepField;
import net.bolue.sysmng.tblbean.RepFunc;
import net.bolue.sysmng.tblbean.RepRes;
import net.bolue.sysmng.util.DictUtil;
import net.bolue.web.entry.FormDataOption;
import net.bolue.web.entry.FormElemTag;
import net.bolue.web.util.TagUtil;

/**
 * 报表信息业务类
 * @author Administrator
 *
 */
public class RepInfoModel extends BusiBase{
	
	public RepInfoModel() {
		super();
	}


	public RepInfoModel(HttpServletRequest request) {
		super(request);
	}

	/**
	 * 获得报表信息
	 * @param subSys	子系统
	 * @param repId		报表标识
	 * @return RepInfo	报表信息
	 * @throws BolueException
	 */
	public RepInfo getRepInfo(String subSys, int repId) throws BolueException{
		ReportResModel reportResModel = new ReportResModel();
		RepRes repRes = reportResModel.getReport(subSys, repId);
		if (repRes == null){
			throw new BolueException(10,"没有找到报表" + subSys + "." + repId + "对应的报表资源!");
		}
		RepInfo repInfo = new RepInfo(subSys,repId);
		repInfo.setRepRes(repRes);
		List<RepField> fieldList = reportResModel.getRepFieldList(subSys, repId);
		repInfo.setFieldList(fieldList);
		int fieldSize = 0;
		if(fieldList!=null&&fieldList.size()>0){
			fieldSize = fieldList.size();
		}
		
		List<RepCnd> cndList = reportResModel.getRepCndList(subSys, repId);
		int cndFieldSize = 0;
		if(cndList!=null&&cndList.size()>0){
			cndFieldSize = cndList.size();
		}
		List<FormElemTag> cndTagList = new ArrayList<FormElemTag>();
		String[] subSysArray = new String[fieldSize+cndFieldSize];
		Integer[] dicKindArray = new Integer[fieldSize+cndFieldSize];
		Integer[] dicGrpIdArray = new Integer[fieldSize+cndFieldSize];
		int k = 0;
		for(int i=0;i<fieldSize;i++,k++){
			RepField repField = fieldList.get(i);
			subSysArray[k] = repField.getSubSys();
			dicKindArray[k] = repField.getDicKind();
			dicGrpIdArray[k] = repField.getDicGrpId();
		}
		if(cndList!=null&&cndList.size()>0){
			for(RepCnd repCnd :cndList){
				cndTagList.add(TagUtil.cndToField(repCnd));
				//将字典信息放入数组中
				subSysArray[k] = repCnd.getSubSys();
				dicKindArray[k] = repCnd.getDicKind();
				dicGrpIdArray[k] = repCnd.getDicGrpId();
				k++;
			}
		}
		repInfo.setCndList(cndTagList);
		Map<String, List<FormDataOption>> dicsMap = DictUtil.getDicMaps(subSysArray, dicKindArray, dicGrpIdArray);
		repInfo.setDicsMap(dicsMap);
		List<RepFunc> funcList = getRepFunc(subSys,repId);
		repInfo.setFuncList(funcList);
		return repInfo;
	}
	

	/**
	 * 获得报表功能按钮
	 * @param subSys	子系统
	 * @param repId		报表标识
	 * @return List		功能按钮
	 * @throws BolueException
	 */
	public List<RepFunc> getRepFunc(String subSys, int repId) throws BolueException{
		ReportResModel reportResModel = new ReportResModel();
		List<RepFunc> funcList = reportResModel.getUserRepFuncList(subSys, repId);
		if(funcList==null||funcList.size()==0){
			funcList = new ArrayList<RepFunc>();
		}
		RepFunc backFunc = new RepFunc(subSys,repId,"back","返回",funcList.size(),11);
		funcList.add(backFunc);
		return funcList;
	}
}
